<template>
	<div class="wrapper">
		<!--header-->
		<header>
			<div class="back">
				<p @click="goBack">&lt;</p>
			</div>
			<p>确认订单</p>
		</header>
		<!--info-->
		<div class="orderInfo">
			<h5>订单配送至：</h5>
			<div class="orderInfo-address" @click="toUserAddress">
				<p>{{diliveryAddress!=null?diliveryAddress.address:'请选择收货地址'}}</p>
				<i class="fa fa-angle-right"></i>
			</div>
			<p v-if="this.diliveryAddress!=null">{{diliveryAddress.contactName}}{{sexJudge(diliveryAddress.contactSex)}} {{diliveryAddress.contactTel}}</p>
		</div>
		<h3>{{business.businessName}}</h3>

		<!--orderDetail-->
		<ul class="orderDetail">
			<li v-for="item of cartArr">
				<div class="orderDetail-l">
					<img :src="item.food.foodImg">
					<p>{{item.food.foodName}} x {{item.quantity}}</p>
				</div>
				<p>&#165;{{item.quantity*item.food.foodPrice}}</p>
			</li>
		</ul>
		<div class="order-deliverFee">
			<p>配送费</p>
			<p>&#165;{{business.deliveryPrice}}</p>
		</div>
		<div class="discount" v-if="isDiscountAvlb">
			<p>积分抵扣</p>
			<p style="color: red;">-&#165;{{this.discountAVLB}}</p>
		</div>
		<div class="discount" v-if="!isDiscountAvlb">
			<p>积分抵扣</p>
			<p>可用积分不足</p>
		</div>

		<!--total-->
		<div class="total">	
			<div class="total-l">
				&#165;{{totalPrice}}
			</div>
			<div class="total-r" @click="toPayment">
				去支付
			</div>
		</div>
	</div>
</template>

<script>
	export default{
		name:'Order',
		data(){
			return{
				businessId:this.$route.query.businessId,
				business:{},
				user:{},
				cartArr:[],
				diliveryAddress:{},
				isDiscountAvlb:false,
				pointAVLB:0,
				discountAVLB:0,
			}
		},
		created(){
			this.user = this.$getSessionStorage('user');
			//取得收货地址
			this.diliveryAddress =this.$getLocalStorage(this.user.userId);
			
			
			//根据businessId查询商家信息
			this.$axios.post('BusinessController/getBusinessById',this.$qs.stringify({
				businessId:this.businessId
			})).then(response=>{
				this.business = response.data;
			}).catch(error=>{
				console.error(error);
			});
			//查询购物车
			this.$axios.post('CartController/listCart',this.$qs.stringify({
				userId:this.user.userId,
				businessId:this.businessId
			})).then(response=>{
				this.cartArr = response.data;
			}).catch(error=>{
				console.error(error);
			});
			//获取可用积分
			this.$axios.post('PointController/getPointById',this.$qs.stringify({
				userId:this.user.userId
			})).then(response=>{
				this.pointAVLB = response.data;
				//console.log(this.pointAVLB);
				if(this.pointAVLB>=10)
				{
					this.isDiscountAvlb=true;
					this.getDiscountA();
					//console.log(this.discountAVLB);
					
				}
			}).catch(error=>{
				console.error(error);
			});
		},
		computed:{
			totalPrice() {
				let totalPrice = 0;
				for(let cartItem of this.cartArr){
					totalPrice+=cartItem.quantity * cartItem.food.foodPrice;
				}
				totalPrice += this.business.deliveryPrice;
				if(this.isDiscountAvlb){
					totalPrice -= this.discountAVLB;
				}
				return totalPrice;
			}
		},
		filters:{
			sexFilter(sex){
				return sex==1?'先生':'女士';
			}
		},
		methods:{
			sexJudge(sex){
				return sex === 1 ? '先生':'女士';
			},
			    goBack() {
			      this.$router.go(-1);
			    },
			toUserAddress(){
				this.$router.push({path:'/userAddress',query:{ businessId:this.businessId}});
			},
			toPayment(){
				if(this.diliveryAddress==null){
					alert('请选择收货地址');
					return;
				}
				
				if(this.isDiscountAvlb){
					this.updatePoint();
				}
				
				//创建订单
				this.$axios.post('OrdersController/createOrders',this.$qs.stringify({
					userId:this.user.userId,
					businessId:this.businessId,
					daId:this.diliveryAddress.daId,
					orderTotal:this.totalPrice
				})).then(response=>{
					let orderId = response.data;
					if(orderId>0){
						this.$router.push({path:'/payment',query:{orderId:orderId}});
					}
					else{
						alert('创建订单失败');
					}
				}).catch(error=>{
					console.error(error);
				});
			},
			getDiscountA(){
				if(this.pointAVLB>=100){
					this.discountAVLB=10;
				}else{
					let tPoint =this.pointAVLB-this.pointAVLB%10;
					this.discountAVLB=tPoint/10;
				}
			},
			updatePoint(){
				let temp=this.discountAVLB*10;
				let newPoint=this.pointAVLB-temp;
				//console.log(newPoint);
				this.$axios.post('PointController/updateUserPoint', this.$qs.stringify({
					userId:this.user.userId,point:newPoint}
				)).then(response => {
					if (response.data > 0) {
						alert('您花费了'+temp+'个积分');
					} else {
						alert('积分更新失败！');
					}
				}).catch(error => {
					console.error(error);
				});
			}
		}/*
		data() {
			return {
				businessId: this.$route.query.businessId,
				business: {},
				user: {},
				cartArr: [],
				deliveryaddress: {}
			}
		},
		created() {
			this.user = this.$getSessionStorage('user');
			this.deliveryaddress = this.$getLocalStorage(this.user.userId);
		
			//查询当前商家
			this.$axios.post('BusinessController/getBusinessById', this.$qs.stringify({
				businessId: this.businessId
			})).then(response => {
				this.business = response.data;
			}).catch(error => {
				console.error(error);
			});
		
			//查询当前用户在购物车中的当前商家食品列表
			this.$axios.post('CartController/listCart', this.$qs.stringify({
				userId: this.user.userId,
				businessId: this.businessId
			})).then(response => {
				this.cartArr = response.data;
			}).catch(error => {
				console.error(error);
			});
		},
		computed: {
			totalPrice() {
				let totalPrice = 0;
				for (let cartItem of this.cartArr) {
					totalPrice += cartItem.food.foodPrice * cartItem.quantity;
				}
				totalPrice += this.business.deliveryPrice;
				return totalPrice;
			}
		},
		//过滤器，根据value显示性别
		filters: {
			sexFilter(value) {
				return value == 1 ? '先生' : '女士';
			}
		},
		methods: {
			//前往用户地址
			toUserAddress() {
				this.$router.push({
					path: '/userAddress',
					query: {
						businessId: this.businessId
					}
				});
			},
			//去支付
			toPayment() {
				if (this.deliveryaddress == null) {
					alert('请选择送货地址！');
					return;
				}
		
				//创建订单
				this.$axios.post('OrdersController/createOrders', this.$qs.stringify({
					userId: this.user.userId,
					businessId: this.businessId,
					daId: this.deliveryaddress.daId,
					orderTotal: this.totalPrice
				})).then(response => {
					let orderId = response.data;
					if (orderId > 0) {
						this.$router.push({
							path: '/payment',
							query: {
								orderId: orderId
							}
						});
					} else {
						alert('创建订单失败！');
					}
				}).catch(error => {
					console.error(error);
				});
			}
		}*/
	}
</script>

<style scoped>
	/****************总容器*********************/

	.wrapper {
		width: 100%;
		height: 100%;

	}

	/****************header*********************/
	.wrapper header {
		width: 100%;
		height: 12vw;
		background-color: #0097FF;
		color: #FFFFFF;
		font-size: 4.8vw;

		position: fixed;
		left: 0;
		top: 0;
		z-index: 1000;

		display: flex;
		align-items: center;
		justify-content: center;
	}

	.wrapper header .back {
		position: absolute;
		width: 2vw;
		height: 2vw;
		left: 1.5vw;
		top: 2.8vw;
		user-select: none;
		cursor: pointer;
	}

	/*******orderInfo*******/
	.wrapper .orderInfo {
		width: 100%;
		margin-top: 12vw;
		background-color: #0097EF;
		box-sizing: border-box;
		padding: 2vw;
		color: #FFFFFF;
	}

	.wrapper .orderInfo h5 {
		font-size: 3vw;
		font-weight: 300;
	}

	.wrapper .orderInfo .orderInfo-address {
		width: 100%;
		display: flex;
		justify-content: space-between;
		align-items: center;

		font-weight: 700;
		user-select: none;
		cursor: pointer;
		margin: 1vw 0;
	}

	.wrapper .orderInfo .orderInfo-address p {
		width: 90%;
		font-size: 5vw;
	}

	.wrapper .orderInfo .orderInfo-address i {
		font-size: 6vw;
	}

	.wrapper .orderInfo p {
		font-size: 3vw;
	}

	.wrapper h3 {
		box-sizing: border-box;
		padding: 3vw;
		font-size: 4vw;
		color: #666666;
		border-bottom: solid 1px #DDDDDD;
	}

	/*******orderDetail*******/
	.wrapper .orderDetail {
		width: 100%;
	}

	.wrapper .orderDetail li {
		width: 100%;
		height: 16vw;
		box-sizing: border-box;
		padding: 3vw;
		color: #666666;

		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.wrapper .orderDetail li .orderDetail-l {
		display: flex;
		align-items: center;
	}

	.wrapper .orderDetail li .orderDetail-l img {
		width: 10vw;
		height: 10vw;
		border-radius: 2.5px;
	}

	.wrapper .orderDetail li .orderDetail-l p {
		font-size: 3.5vw;
		margin-left: 3vw;
	}

	.wrapper .orderDetail li p {
		font-size: 3.5vw;
	}

	.wrapper .order-deliverFee {
		width: 100%;
		height: 16vw;
		box-sizing: border-box;
		padding: 3vw;
		color: #666666;
		font-size: 3.5vw;
		border-top: solid 1px #DDDDDD;

		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	
	.wrapper .discount{
		width: 100%;
		height: 16vw;
		box-sizing: border-box;
		padding: 3vw;
		color: #666666;
		font-size: 3.5vw;
		border-top: solid 1px #DDDDDD;
		
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	
	/*******total*******/
	.wrapper .total {
		width: 100%;
		height: 14vw;

		position: fixed;
		left: 0;
		bottom: 0;

		display: flex;
	}

	.wrapper .total .total-l {
		flex: 2;
		background-color: #444444;
		color: #FFFFFF;
		font-size: 4.5vw;
		font-weight: 700;
		user-select: none;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.wrapper .total .total-r {
		flex: 1;
		background-color: #38CA73;
		color: #FFFFFF;
		font-size: 4.5vw;
		font-weight: 700;
		user-select: none;
		cursor: pointer;
		display: flex;
		justify-content: center;
		align-items: center;
	}
</style>
